package controllers

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"personnel-management/dao/mysql"
	"personnel-management/models"
	"strconv"
)

var (
	casbins = models.CasbinModel{}
)

func AddCasbin(c *gin.Context) {
	//1.获取博客ID
	idStr := c.Param("userid") //获取URL参数
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		ResponseError(c, CodeInvalidParams)
		return
	}
	// 获取作者Name，当前请求的UserName
	userName, err := getCurrentUserName(c)
	if err != nil {
		zap.L().Error("GetCurrentUserName() failed", zap.Error(err))
		ResponseError(c, CodeNotLogin)
		return
	}
	rolename := userName
	path := c.PostForm("path")
	method := c.PostForm("method")
	ptype := "p"
	casbin := models.CasbinModel{
		UserID:   id,
		Ptype:    ptype,
		RoleName: rolename,
		Path:     path,
		Method:   method,
	}
	isok := mysql.AdCasbin(casbin)
	if isok {
		c.JSON(http.StatusOK, gin.H{
			"success": true,
			"msg":     "保存成功",
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"success": false,
			"msg":     "保存失败",
		})
	}

}
